﻿using B.Auth.APi.Application.Command.Leaves;
using B.Auth.APi.Application.Command.Leaves.LeaveDto;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace B.Auth.APi.Application.CommandHand.Leaves
{
    public class GetLeaveshowCommandHand : IRequestHandler<GetLeaveshowCommand, ApiResult<ApiPaging<List<LeaveshowDto>>>>
    {

        private readonly IBaseRepository<LeaveModel> _leave;
        private readonly IBaseRepository<DepartmentModel> _depart;
        private readonly IBaseRepository<User> _user;
        private readonly IBaseRepository<RoleUser> _roleuser;
        private readonly IBaseRepository<RoleModel> _role;


        public GetLeaveshowCommandHand(IBaseRepository<LeaveModel> leave, IBaseRepository<DepartmentModel> depart, IBaseRepository<User> user, IBaseRepository<RoleUser> roleuser, IBaseRepository<RoleModel> role)
        {
            _leave = leave;
            _depart = depart;
            _user = user;
            _roleuser = roleuser;
            _role = role;
        }

        public Task<ApiResult<ApiPaging<List<LeaveshowDto>>>> Handle(GetLeaveshowCommand request, CancellationToken cancellationToken)
        {
           var result= new ApiResult<ApiPaging<List<LeaveshowDto>>>();
            List<LeaveshowDto> dto = new List<LeaveshowDto>();
            var leavelist = _leave.GetAll().ToList();
            foreach (var item in leavelist)
            {
                dto.Add(new()
                {

                    Id = item.Id,
                    LeaveName = item.LeaveName,
                    Department = _depart.GetAll().FirstOrDefault(x => x.Departid == item.Department)?.DepartName ?? "",
                    OptionType = item.OptionType,
                    LeaveTimeMessage = item.BeginTime.ToString("yyyy-MM-dd HH:mm:ss") + "至" + item.EndTime.ToString("yyyy-MM-dd HH:mm:ss"),
                    Departmentid = item.Department,
                    WorkTime = GetworkTime(item.BeginTime,item.EndTime),
                    CheckTime = item.CheckTime.ToString("yyyy-MM-dd"),
                    leaveReason = item.leaveReason,
                    DepartmentHead = item.DepartmentHead,
                    BeginTime = item.BeginTime,
                    EndTime = item.EndTime,
                    Status = item.Status,
                    CreateTime = item.CreateTime,
                    CreateUserName = item.CreateUserName,
                    Del = item.Del,
                    ShenType=item.ShenType,
                    LeaveError = item.LeaveError,
                    AuditNowName =item.AuditNowName,


                });
                
                
                
            }
            var list = dto.Where(x => x.Del == 0).ToList();
            if (!string.IsNullOrEmpty(request.Name))
            {
                list = list.Where(x => x.LeaveName.Contains(request.Name)).ToList();
            
            }
            if (request.Status!=null)
            {
                list = list.Where(x => x.Status == request.Status).ToList();
            }
            if (request.StartTime != null)
            { 
                list=list.Where(x=>x.BeginTime>=request.StartTime).ToList();
            }
            if (request.EndTime != null)
            {
                request.EndTime = request.EndTime.Value.AddDays(1);
                list = list.Where(x => x.EndTime < request.EndTime).ToList();
            
            }
            var count=list.Count();
            var pcount = (int)Math.Ceiling(count * 1.0 / request.size);

            List<int> roleid = _roleuser.GetAll().Where(x => x.UserId == request.userid).Select(x => x.RoleId).ToList();
            List<int?> boos=new List<int?>();
            int? a = 0;
            foreach (var item in roleid)
            {

               a=  _role.GetAll().FirstOrDefault(x => x.Id == item)?.Boos;

                boos.Add(a);
            }
            if (boos.Contains(1))
            {

                var page = list.OrderByDescending(x => x.Id).Skip((request.index - 1) * request.size).Take(request.size);
                var data = new ApiPaging<List<LeaveshowDto>>();

                data.totalcount = count;
                data.totalpcount = pcount;
                data.data = page.ToList();
                result.Code = ApiEnum.ok;
                result.Message = "有审批权限";
                result.data = data;


            }
            else
            {


                var page= list.OrderByDescending(x => x.Id).Skip((request.index - 1) * request.size).Take(request.size).Where(x=>x.LeaveName==request.LoginName);
                var data = new ApiPaging<List<LeaveshowDto>>();

                data.totalcount = count;
                data.totalpcount = pcount;
                data.data = page.ToList();
                result.Code = ApiEnum.ok;
               
                result.data = data;
            }



            return Task.FromResult(result);


        }
        public int GetworkTime(DateTime BeginTime, DateTime Endtime)
        {
            var housetime = (int)(Endtime-BeginTime).TotalHours;
            if (housetime <= 8)
            {

             
                return housetime;
            }
            if (housetime > 8 && housetime <= 24)
            {
                return 8;
            }
            if (housetime > 24)
            {
                var housetime1 = housetime / 24;
                var housetime2 = housetime % 24;
                if (housetime2 <= 8)
                {


                    housetime2 = housetime2;
                }
                if (housetime2 > 8 && housetime < 24)
                {
                    housetime2 = 8;
                }
                housetime = housetime1 * 8 + housetime2;
                return housetime;
                
            }

            return housetime;
        }
      
    }
}
